package net.easyjava.admin.controller;

import net.easyjava.base.bean.ArticleBean;
import net.easyjava.base.constants.AlbumConstants;
import net.easyjava.base.manager.ArticleManager;
import net.easyjava.tools.http.HttpTools;
import net.easyjava.tools.model.ModelBean;
import net.easyjava.tools.model.ModelConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wenxiaoning on 15/12/18.
 */
@Controller
@RequestMapping("/admin/article")
public class ArticleController extends BaseController {
    private  Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    @Resource
    private ArticleManager articleManager;
    @Value("${article.address}")
    private String ARTICLE_ADDRESS;


    /**
     * 文章列表
     * @PARAM REQUEST
     * @PARAM RESPONSE
     * @RETURN
     */
    @RequestMapping("/list.html")
    @ResponseBody
    public ModelAndView list(
            ArticleBean articleBean,
            @RequestParam(required = false,defaultValue = "1") int pageNum,
            @RequestParam(required = false,defaultValue = "10") int pageSize,
            HttpServletRequest request, HttpServletResponse response) {
        String message = ModelConstants.MESSAGE_ERROR;
        try {

            Map<String,Object> params = new HashMap<String,Object>();
            params.put("page",articleManager.findList(articleBean, pageNum, pageSize));

            return new ModelAndView("admin/article/list.html",params);
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }

    }


    /**
     * 显示修改页面
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/edit.html")
    @ResponseBody
    public ModelAndView editHtml(
            @RequestParam(required = false,defaultValue = "") String id,
            HttpServletRequest request, HttpServletResponse response) {
        String message = ModelConstants.MESSAGE_ERROR;
        try {

            Map<String,Object> params = new HashMap<String,Object>();
            if(!"".equals(id)){
                ModelBean modelBean = articleManager.findOne(request, response);
                if(modelBean.getStatus() == ModelConstants.STATUS_ERROR){
                    throw new Exception();
                }
                ArticleBean articleBean = (ArticleBean) modelBean.getData();
                params.put("article", articleBean);

                params.put("content", HttpTools.get(articleBean.getPath()));
            }

            params.put("albums", AlbumConstants.values());

            return new ModelAndView("admin/article/edit.html",params);
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }

    }


    /**
     * 添加或修改文章
     * @param articleBean
     * @param content
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/edit.do")
    @ResponseBody
    public ModelBean edit(
            ArticleBean articleBean,
            @RequestParam String content,
            HttpServletRequest request, HttpServletResponse response) {
        String message = ModelConstants.MESSAGE_ERROR;
        try {
            String userId = this.getUserId();
            if(userId == null){
                userId = "";
            }

            articleBean.setUserId(userId);
            return model(articleManager.edit(articleBean,content),request,response);
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return model(new ModelBean(ModelConstants.STATUS_SUCCESS,message),request,response);
        }

    }








}
